package com.example.myapplication.old.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.old.entity.Goods;

import java.util.List;


public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.GoodsHolder> {
    private List<Goods>Goodslist;

    public void setDate(List<Goods> Goodslist){
        this.Goodslist=Goodslist;
        notifyDataSetChanged();
    }
    public GoodsAdapter() {
    }

    @NonNull
    @Override
    public GoodsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods,parent,false);
        return new GoodsHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsHolder holder, int position) {
        Goods good=Goodslist.get(position);
        holder.imggoods.setImageResource(good.getImg());
        holder.tvgoodsname.setText(good.getName());
        holder.tvprice.setText("￥"+good.getPrice()+"元");
        holder.tvcount.setText("在售"+good.getCount()+"件");
    }

    @Override
    public int getItemCount() {
        return Goodslist == null? 0 : Goodslist.size();
    }

    class GoodsHolder extends RecyclerView.ViewHolder{
        ImageView imggoods;
        TextView tvgoodsname;
        TextView tvprice;
        TextView tvcount;
        public GoodsHolder(@NonNull View itemView) {
            super(itemView);
            imggoods=itemView.findViewById(R.id.img_goods);
            tvgoodsname=itemView.findViewById(R.id.tv_goodname);
            tvprice=itemView.findViewById(R.id.tv_price);
            tvcount=itemView.findViewById(R.id.tv_count);
        }
    }
}
